<?php
namespace app\common\only;

class Cart extends Model {
	protected $prefix = 'Cart';

	public function order4store($ids) {
		return $this->get('order4store', function($ids) {
			$list = loadModel('tyyh\\UserCart')->whereIn('id', $ids)->lock(true)->select();
			$sids = [];
			$pids = [];
			$psids = [];
			$total_fee = 0;
			$commission = 0;
			if(count($list) > 0) {
				foreach($list as $k => $v) {
					if(!isset($sids[$v['sid']])) {
						$sids[$v['sid']] = loadModel('tysj\\Store')->find($v['sid']);
					}
					if(!isset($pids[$v['pid']])) {
						$pids[$v['pid']] = loadModel('tysj\\Product')->find($v['pid']);
					}
					if(!isset($psids[$v['psid']])) {
						$row = $psids[$v['psid']] = loadModel('tysj\\ProductSpecs')->find($v['psid']);
					}
					$total_fee += $row['price'] * $v['num'];
					$commission += $row['commission'] * $v['num'];
				}
			}
			return ['sids' => $sids, 'pids' => $pids, 'psids' => $psids, 'total_fee' => $total_fee, 'commission' => $commission, 'list' => $list];
		}, [$ids]);
	}
}
